package com.elinkway.infinitemovies.adapter;

import android.content.Context;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;

import com.elinkway.infinitemovies.bean.MainHeaderBean;
import com.elinkway.infinitemovies.selfdata.bean.LaunchDurationDataRecord;
import com.elinkway.infinitemovies.ui.fragment.HomeTestFragment;

import java.util.List;

public class MainFragmentStatePagerAdapter extends FragmentPagerAdapter{

	private List<MainHeaderBean> headerList;
	private Context mContext;
	private HomeTestFragment mCurrentFragment;
	private String defaultPage; // 默认选中的频道page
	private LaunchDurationDataRecord launchDuration; // 启动时长日志

	public MainFragmentStatePagerAdapter(FragmentManager fm, List<MainHeaderBean> headerList, Context context,String defaultPage,LaunchDurationDataRecord launchRecord) {
		super(fm);
		this.headerList = headerList;
		this.mContext = context;
		this.defaultPage = defaultPage;
		this.launchDuration = launchRecord;
	}

	public void setHeaderList(List<MainHeaderBean> headerList){
		this.headerList = headerList;
		notifyDataSetChanged();
	}

	@Override
	public Fragment getItem(int position) {
		if(headerList == null || headerList.get(position) == null){
			return null;
		}
		return HomeTestFragment.newInstance(headerList.get(position),defaultPage,launchDuration);
	}

	@Override
	public int getCount() {
		if(headerList == null){
			return 0;
		}
		return headerList.size();
	}

	@Override
	public CharSequence getPageTitle(int position) {
		if(headerList == null || headerList.get(position) == null){
			return "标题";
		}
		return headerList.get(position).getName();
	}

	@Override
	public void setPrimaryItem(ViewGroup container, int position, Object object) {
		mCurrentFragment = (HomeTestFragment) object;
		super.setPrimaryItem(container, position, object);
	}

	public HomeTestFragment getCurrentFragment() {
		return mCurrentFragment;
	}

	public int getItemPosition(Object object){
		return POSITION_NONE;
	}

	@Override
	public void restoreState(Parcelable state, ClassLoader loader) {
		return;
	}
}
